package algorithm

import "fmt"

// 将二维矩阵翻转90
func reverNinthv1(arr [][]int) {
	if (len(arr) == 0 || len(arr[0]) == 0) || (len(arr) != len(arr[0])) {
		return
	}
	// 先转置
	for i := 0; i < len(arr); i++ {
		for j := i + 1; j < len(arr[i]); j++ {
			tmp := arr[i][j]
			arr[i][j] = arr[j][i]
			arr[j][i] = tmp
		}
	}
	// 再平移
	n := len(arr)
	for i := 0; i < n; i++ {
		for j := 0; j < n/2; j++ {
			tmp := arr[i][j]
			arr[i][j] = arr[i][n-1-j]
			arr[i][n-1-j] = tmp
		}
	}
	return
}

func reverNinthv2(arr [][]int) {
	n := len(arr)
	if n == 0 {
		return
	}
	res := [][]int{}
	for i := 0; i < len(arr); i++ {
		for j := 0; j < n; j++ {
			res[j][n-1-j] = arr[i][j]
		}
	}
	if r := recover(); r != nil {
		fmt.Println("出bug了！！")
	}
}

func RunReverNith() {
	arr := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
	fmt.Println("运行算法题，翻转90二维数组！！！")
	fmt.Println("初始数组")
	Print(arr)
	fmt.Println("翻转后")
	reverNinthv1(arr)

	Print(arr)
}
func Print(arr [][]int) {
	for i := 0; i < len(arr); i++ {
		for j := 0; j < len(arr[i]); j++ {
			fmt.Printf("%2d", arr[i][j])
		}
		fmt.Println()
	}
}
